package com.y.simple.dto;

import java.io.Serializable;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.y.simple.utils.StringUtil;
import com.y.simple.utils.WrapperUtil;
import com.y.simple.entity.UserMain;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

/**
<h1>UserMainQueryDto</h1>
* <ul>
*   <li>前端向后端传递的查询参数</li>
*   <li>简单的查询设置在方法 wrapper 中设置</li>
* </ul>
*
* @author y2014188432
* @since 2023-05-23
*/
@Data
@Schema(name = "UserMainQueryDto", description = "")
public class UserMainQueryDto implements Serializable {
    /**
    * id
    */
    @Schema(name = "id", description = "id")
    private Integer id;

    /**
    * 用户名
    */
    @Schema(name = "userName", description = "用户名")
    private String userName;

    /**
    * 用户手机号
    */
    @Schema(name = "userPhone", description = "用户手机号")
    private String userPhone;

    /**
    * 用户邮箱
    */
    @Schema(name = "userEmail", description = "用户邮箱")
    private String userEmail;

    /**
    * 用户密码
    */
    @Schema(name = "userPassword", description = "用户密码")
    private String userPassword;

    /**
    * 身份
    */
    @Schema(name = "userType", description = "身份")
    private String userType;

    /**
    * 用户状态
    */
    @Schema(name = "userState", description = "用户状态")
    private String userState;

    /**
    * 创建时间
    */
    @Schema(name = "createTime", description = "创建时间")
    private String createTime;

    /**
    * 每页大小
    */
    @Schema(name = "pageSize",description = "每页大小")
    private Integer pageSize;

    /**
    * 页码
    */
    @Schema(name = "pageNumber",description = "页码")
    private Integer pageNumber;

    /**
    * <h1>排序规则</h1>
    * <h1>[ASC/DESC].[字段名];</h1>
    * <ul>
    *     <li>中括号内为代填项</li>
    *     <li>排序规则与排序项用英文点分割</li>
    *     <li>每组用英文分隔符进行隔离</li>
    *     <li>不要出现空格</li>
    * </ul>
    */
    @Schema(name = "orderRule",description = "排序规则[ASC/DESC].[字段名];")
    private String orderRule;

    private QueryWrapper<UserMain> wrapper() {
        // 初始化
        QueryWrapper<UserMain> wrapper = new QueryWrapper<>();
        // 添加查询条件
        if (StringUtil.hasEmpty(userName)) {
            wrapper.like("user_name",userName);
        }
        if (StringUtil.hasEmpty(userPhone)) {
            wrapper.like("user_phone",userPhone);
        }
        if (StringUtil.hasEmpty(userEmail)) {
            wrapper.like("user_email",userEmail);
        }
        if (StringUtil.hasEmpty(userPassword)) {
            wrapper.like("user_password",userPassword);
        }
        if (StringUtil.hasEmpty(userType)) {
            wrapper.like("user_type",userType);
        }
        if (StringUtil.hasEmpty(userState)) {
            wrapper.like("user_state",userState);
        }
        // 不显示已删除内容
        wrapper.eq("is_delete","0");
        // 返回
        return wrapper;
    }


    public QueryWrapper<UserMain> countWrapper() {
        return wrapper();
    }

    public QueryWrapper<UserMain> queryWrapper() {
        QueryWrapper<UserMain> wrapper = wrapper();
        // 排序
        new WrapperUtil< UserMain >().orderUtil(wrapper,orderRule);
        // 添加分页查询
        if (pageSize != null && pageNumber != null && pageSize > 0 && pageNumber > 0) {
            int pageStart = (pageNumber - 1) * pageSize;
            wrapper.last("limit " + pageStart + "," + pageSize);
        } else {
            wrapper.last("limit 0,15");
        }
        return wrapper;
    }
}
